﻿using System;
using System.Linq;
using KPIS.GERP.Models.SYS.ViewModel;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.Models;

namespace KPIS.GERP.WCF.SYS
{
    public partial class SysApprovalService
    {

        private int SendMessageSend(int taskHistoryId, string taskRefCode, string title, int fromEmpId, int fromPosId, int? toEmpId, int? toPosId, string thRemark)
        {
            var db = new GERPContext(true);
            MessageSend messageSend = new MessageSend();
            messageSend.SendDate = DateTime.Now;
            messageSend.RefId = taskHistoryId;
            messageSend.RefType = "sys_task";
            messageSend.RefNo = taskRefCode;
            messageSend.SendText = title;
            messageSend.FromEmpId = fromEmpId;
            messageSend.FromPosId = fromPosId;
            messageSend.ToEmpId = toEmpId;
            messageSend.ToPosId = toPosId;
            messageSend.WodEmpId = null;
            messageSend.FromSendId = null;
            messageSend.Remarks = thRemark;
            messageSend.ApprovePathId = null;
            messageSend.MessageStatus = "0";
            messageSend.RecordStatus = "U";
            messageSend.CreatedBy = fromEmpId;
            messageSend.CreatedWhen = DateTime.Now;
            db.MessageSends.Add(messageSend);
            db.SaveChanges();
            return messageSend.SendId;
        }

        public UnwarnTaskCountVM CountUnwarnMessage(int empId, bool markAsWarn = true)
        {
            var context = new GERPContext(true);
            var emp = context.Employees.Find(empId);
            var pos = emp.Position;
            var result = new UnwarnTaskCountVM(empId, pos.PosId);

            foreach (var t in context.Tasks.Where(o => o.RecordStatus != "D"))
            {
                if (t.ApplyToEmpId == empId || t.CreatedBy == empId)
                {
                    result.MyRequestAllUnwarnTaskIds.Add(t.TaskId);
                    result.MyRequestAllUnreadTaskIds.Add(t.TaskId);
                }

                if ((t.ApplyToEmpId == empId || t.CreatedBy == empId) && t.Completed && !t.CanceledBy.HasValue)
                {
                    result.MyRequestResponsedUnwarnTaskIds.Add(t.TaskId);
                    result.MyRequestResponsedUnreadTaskIds.Add(t.TaskId);
                }

                if ((t.ApplyToEmpId == empId || t.CreatedBy == empId) && t.Completed && t.CanceledBy.HasValue)
                {
                    result.MyRequestCanceledUnwarnTaskIds.Add(t.TaskId);
                    result.MyRequestCanceledResponsedUnreadTaskIds.Add(t.TaskId);
                }

                if ((from th in t.TaskHistories
                     where th.RecordStatus != "D" && th.ApproverPosId == pos.PosId
                     select th).Any())
                {
                    result.AllUnwarnTaskIds.Add(t.TaskId);
                    result.AllUnreadTaskIds.Add(t.TaskId);
                }

                if ((from th in t.TaskHistories
                     where th.RecordStatus != "D" && th.PassStep
                           && th.ApproverPosId == pos.PosId
                     select th).Any())
                {
                    result.ResponsedUnwarnTaskIds.Add(t.TaskId);
                    result.ResponsedUnreadTaskIds.Add(t.TaskId);

                }
            }
            return result;
        }
    }
}